<?php
/**
 * 	合同管理模板
 *  author： chengda
 *  date  ： 2017-11-22
 */
namespace Admin\Controller;

class GeneralApplyController extends AbstractController  {

	protected $From;

	public function _initialize(){
		$this->Form = D('generalapply');
    }
	

    public function save($type = false, $id = 0){
		$this->display();
    }

    public function edit(){
    	if(I('ID'))
    	{
			$SQL="SELECT
			A.ID AS ID,
			A.TID AS TID,
			B.TNO AS TIDName,
			A.DepID  AS DepID,
			C.`Name`  AS DepName,
			A.UserID AS UserID,
			D.UserName AS UserName,
			A.Title AS Title,
			A.Content AS Content,
			A.CopyUser AS CopyUser,
			A.ApplyTime AS ApplyTime,
			(SELECT GROUP_CONCAT(CONCAT(UserName)) FROM 
			`user` WHERE   find_in_set(ID,A.CopyUser)) AS CopyUserName,
			A.PrintUserID AS PrintUserID,
			E.UserName AS PrintUserName
			FROM
			generalapply AS A
			LEFT JOIN task AS B ON (A.TID = B.ID) 
			LEFT JOIN department AS C ON(A.DepID=C.ID)
			LEFT JOIN `user` AS D ON(A.UserID=D.ID)
			LEFT JOIN `user` AS E ON(A.PrintUserID=E.ID)
			WHERE A.ID=";
			$res = $this->Form->query($SQL.(int)I('ID'));
			$this->assign("editdata",$res[0]);
		}
		$this->display("save");
    }

    public function add()
    {
		if(!$this->Form->create())
		{
			exit($this->error($this->Form->getError()));
		}
		else
		{
			$result = $this->Form->add();

			if($result)
			{
				$this->success('添加成功！');
			}
			else
			{
				$this->error('添加错误！');
			}
		}
	}

	function update(){

		if(!$this->Form->create())
		{
			exit($this->error($this->Form->getError()));
		}
		else
		{
			$result = $this->Form->save();

			if($result)
			{
				$this->success('修改成功！');
			}
			else
			{
				$this->error('修改错误！');
			}
		}
	}

    // 选择任务
    public function tasklist(){
        $where['t.IsDel'] = array('neq','1');
        $count = M('Task as t')->where($where)->count();
        $keyword = I('keyword');
        if(!empty($keyword)){
            $where['t.TNO|t.Name|u.TrueName'] = array('like','%'.$keyword.'%');
        }
        $page = $this->page($count, 10);
        $fields = 't.*,u.TrueName as UserName,b.Name as BName';
        $list = M('Task as t')->join('left join user as u on u.ID=t.UserID')
            ->join('left join business as b on b.ID=t.BusiID')
            ->where($where)->order('t.ID desc')->field($fields)->limit($page->firstRow . ',' . $page->listRows)->select();

        $this->assign('status',$this->status);
        $this->assign('list',$list);
        $this->assign("page", $page->show('Admin'));
        $this->display();
    }

   


}